package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.dao.AutoDao;
import io.renren.dao.AutobrandDao;
import io.renren.dao.AutogroupDao;
import io.renren.dao.AutomodelsDao;
import io.renren.dto.AutoDTO;
import io.renren.entity.AutoEntity;
import io.renren.entity.AutobrandEntity;
import io.renren.entity.AutogroupEntity;
import io.renren.entity.AutomodelsEntity;
import io.renren.service.AutoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 车辆信息主表
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-22
 */
@Service
public class AutoServiceImpl extends CrudServiceImpl<AutoDao, AutoEntity, AutoDTO> implements AutoService {

    @Autowired
    private AutobrandDao autobrandDao;
    @Autowired
    private AutogroupDao autogroupDao;
    @Autowired
    private AutomodelsDao automodelsDao;
    @Autowired
    private AutoDao autoDao;

    @Override
    public QueryWrapper<AutoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Boolean isExByFk(String tradermFk) {

        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("TraderM_FK", tradermFk);
        List<AutoEntity> list = baseDao.selectList(wrapper);
        return list.size() > 0 ? true : false;
    }

    @Override
    public AutoEntity findOneByAutoFrameNum(String autoFrameNum) {
        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoFrameNum", autoFrameNum);
        List<AutoEntity> list = baseDao.selectList(wrapper);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public AutoEntity findOneByAutoNo(String autoNo) {
        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoNo", autoNo);
        List<AutoEntity> list = baseDao.selectList(wrapper);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public Long findOneByTraderMFkByTraderMOID(String traderMOID) {
        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("TraderM_FK", traderMOID);
        return baseDao.selectCount(wrapper);
    }

    @Override
    public void createInfo(AutoEntity newInfo) {
        baseDao.insert(newInfo);
    }

    @Override
    public void updateInfo(AutoEntity autoEntity) {
        baseDao.update(autoEntity, new QueryWrapper<AutoEntity>().eq("AutoOID", autoEntity.getAutooid()));
    }

    @Override
    public AutoEntity findOneByAutoOID(String autoFk) {
        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoOID", autoFk);
        List<AutoEntity> list = baseDao.selectList(wrapper);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<Map<String, Object>> getMapByTraderMFk(String traderMOID) {
        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.select("AutoOID,TraderM_FK,AutoNo,AutoBrand_FK,AutoGroup_FK,AutoModels_FK,IsDefault,InternalVehicleSign");
        wrapper.eq("TraderM_FK", traderMOID);
        wrapper.apply("(StopDate is  null or StopDate = '1900-01-01')");
        wrapper.orderByDesc("IsDefault");
        List<Map<String, Object>> autoEntities = baseDao.selectMaps(wrapper);
        for (Map<String, Object> item : autoEntities) {
            QueryWrapper<AutobrandEntity> wrapperBrand = new QueryWrapper<>();
            wrapperBrand.eq("AutoBrandOID", item.get("AutoBrand_FK"));
            List<AutobrandEntity> autobrands = autobrandDao.selectList(wrapperBrand);
            if (autobrands.size() > 0) {
                item.put("img", autobrands.get(0).getAutobrandpic());
            }
            item.put("AutoNo", item.get("AutoNo").toString());

            QueryWrapper<AutobrandEntity> wrapperA = new QueryWrapper<>();
            wrapperA.select("Name");
            wrapperA.eq("AutoBrandOID", item.get("AutoBrand_FK").toString());
            List<AutobrandEntity> autobrandEntities = autobrandDao.selectList(wrapperA);
            if (autobrandEntities.size() > 0) {
                item.put("AutoModelsName", autobrandEntities.get(0).getName());
            }

            QueryWrapper<AutogroupEntity> wrapperB = new QueryWrapper<>();
            wrapperB.select("Name");
            wrapperB.eq("AutoGroupOID", item.get("AutoGroup_FK").toString());
            List<AutogroupEntity> autogroupEntities = autogroupDao.selectList(wrapperB);
            if (autogroupEntities.size() > 0) {
                item.put("AutoModelsName", autogroupEntities.get(0).getName());
            }

            QueryWrapper<AutomodelsEntity> wrapperC = new QueryWrapper<>();
            wrapperC.select("Name,AutoYear");
            wrapperC.eq("AutoModelsOID", item.get("AutoModels_FK").toString());
            List<AutomodelsEntity> autobrandEntities1 = automodelsDao.selectList(wrapperC);
            if (autobrandEntities1.size() > 0) {
                item.put("AutoModelsName", autobrandEntities1.get(0).getAutoyear() + "款" + autobrandEntities1.get(0).getName());
            }

            List<String> autoArr = autoDao.exSql(item.get("AutoOID").toString());
            if (autoArr.size() > 0) {
                item.put("identifyName", autoArr.get(0));
            }
        }
        return autoEntities;
    }

    @Override
    public void setAutoDefault(String traderMOID, String autoOID) {
        AutoEntity autoEntity = new AutoEntity();
        autoEntity.setIsdefault(false);
        baseDao.update(autoEntity, new QueryWrapper<AutoEntity>()
                .eq("TraderM_FK", traderMOID)
                .eq("IsDefault", true));

        autoEntity.setIsdefault(true);
        baseDao.update(autoEntity, new QueryWrapper<AutoEntity>()
                .eq("TraderM_FK", traderMOID)
                .eq("AutoOID", autoOID));
    }

    @Override
    public Map<String, Object> myAutoInfo(String traderMOID, String autoOID) {

        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.select("AutoOID,AutoNo,AutoFrameNum,AutoBrand_FK,AutoModels_FK,AutoGroup_FK,NowMileage,CompulsoryInsuranceEndDate,CommercialInsuranceEndDate,YearlyCheckDate");
        wrapper.eq("AutoOID", autoOID);
        List<Map<String, Object>> autoEntities = baseDao.selectMaps(wrapper);
        if (autoEntities.size() > 0) {
            Map<String, Object> item = autoEntities.get(0);
            List<String> list = baseDao.exSql(item.get("AutoOID").toString());
            if (list.size() > 0) {
                item.put("identifyName", list.get(0));
            }

            QueryWrapper<AutobrandEntity> wrapperBrand = new QueryWrapper<>();
            wrapperBrand.eq("AutoBrandOID", item.get("AutoBrand_FK").toString());
            List<AutobrandEntity> autobrandEntities = autobrandDao.selectList(wrapperBrand);
            if (autobrandEntities.size() > 0) {
                item.put("BrandName", autobrandEntities.get(0).getName());
            }

            QueryWrapper<AutomodelsEntity> wrapperModel = new QueryWrapper<>();
            wrapperModel.eq("AutoModelsOID", item.get("AutoModels_FK").toString());
            List<AutomodelsEntity> automodelsEntities = automodelsDao.selectList(wrapperModel);
            if (automodelsEntities.size() > 0) {
                item.put("ModelsName", automodelsEntities.get(0).getName());
            } else {
                item.put("ModelsName", "");
            }

            item.put("NowMileage", item.get("NowMileage"));

            if (item.get("CompulsoryInsuranceEndDate") != null && !item.get("CompulsoryInsuranceEndDate").equals("1900-01-01 00:00:00.000")) {
                item.put("CompulsoryInsuranceEndDate", DateUtils.parseToStr(item.get("CompulsoryInsuranceEndDate").toString(), "Y-m-d"));
            } else {
                item.put("CompulsoryInsuranceEndDate", "");
            }

            if (item.get("CommercialInsuranceEndDate") != null && !item.get("CommercialInsuranceEndDate").equals("1900-01-01 00:00:00.000")) {
                item.put("CommercialInsuranceEndDate", DateUtils.parseToStr(item.get("CommercialInsuranceEndDate").toString(), "Y-m-d"));
            } else {
                item.put("CommercialInsuranceEndDate", "");
            }

            if (item.get("YearlyCheckDate") != null && !item.get("YearlyCheckDate").equals("1900-01-01 00:00:00.000")) {
                item.put("YearlyCheckDate", DateUtils.parseToStr(item.get("YearlyCheckDate").toString(), "Y-m-d"));
            } else {
                item.put("YearlyCheckDate", "");
            }
            return item;
        }

        return Collections.emptyMap();
    }

    @Override
    public void myAutoEdit(String traderMOID, String autoOID) {

    }

    @Override
    public Long findOneByTraderMFkByAutoFrameNum(String autoframenum) {
        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoFrameNum", autoframenum);
        return baseDao.selectCount(wrapper);
    }

    @Override
    public void deleteByAutoOIDAndTraderMDK(String traderMOID, String autoOID) {
        QueryWrapper<AutoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoOID", autoOID);
        wrapper.eq("TraderM_FK", traderMOID);
        baseDao.delete(wrapper);
    }

    private String HideAutoNo(String autoNo) {
        String[] arr = autoNo.split("-");
        String prefix = arr[0];
        String suffix = arr[1].substring(4, arr[1].length());
        return prefix + "****" + suffix;
    }
}